package com.trm.student.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.trm.student.dao.StudentDao;

public class StudentService {

    private List<Map<String, Object>> properties;

    public List<Map<String, Object>> getProperties() {
        return properties;
    }

    public void setProperties(List<Map<String, Object>> properties) {
        this.properties = properties;
    }

    /**
     * 保存用户信息
     * 
     * @param map
     * @return
     */
    public int saveUser(Map<String, String[]> map) {
        // 根据角色信息获得属性
        String role = map.get("role")[0];
        StudentDao stuDao = new StudentDao();
        String properties = stuDao.getPropertiesByRole(role);
        if (!properties.contains("name;")) {
            properties = "name;" + properties;
        }
        if (!properties.contains("sex;")) {
            properties = properties.replace("name;", "name;sex;");
        }
        String[] property = properties.split(";");
        // 封装参数
        Map<String, String> student = new HashMap<String, String>();
        student.put("role", role);
        for (int i = 0; i < property.length; i++) {
            if (map.get(property[i])[0] != null && !"".equals(map.get(property[i])[0])) {
                student.put(property[i], map.get(property[i])[0]);
            }
        }
        // 保存默认主页
        if (map.get("homepage") != null) {
            student.put("homepage", map.get("homepage")[0]);
        }

        // 保存数据
        StudentDao studentDao = new StudentDao();
        String id = map.get("id")[0];
        int result = 0;
        if (id != null && !"".equals(id)) {
            student.put("id", id);
            result = studentDao.updateUserById(student);
        } else {
            // 设置邮箱为用户账号，手机号为密码
            student.put("account", student.get("mail"));
            student.put("password", student.get("phone"));
            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            list.add(student);
            result = studentDao.addStudent(list);
        }
        return result;
    }

    /**
     * 查询用户角色对应字段
     * 
     * @param role
     * @param page
     * @param pageSize
     * @return
     */
    public List<Map<String, Object>> getUserListByRole(String role, Integer page, Integer pageSize) {
        StudentDao stuDao = new StudentDao();
        String properties = stuDao.getPropertiesByRole(role);
        if (!properties.contains("name;")) {
            properties = "name;" + properties;
        }
        if (!properties.contains("sex;")) {
            properties = properties.replace("name;", "name;sex;");
        }
        // setProperties(properties);
        String propertyIn = "";
        String[] property = properties.split(";");
        for (int i = 0; i < property.length; i++) {
            if (i == property.length - 1) {
                propertyIn += "'" + property[i] + "'";
            } else {
                propertyIn += "'" + property[i] + "',";
            }
        }
        List<Map<String, Object>> propertyList = stuDao.getProperties(propertyIn);
        // 处理属性顺序
        // List<Map<String, Object>> newPropertyList = new ArrayList<Map<String,
        // Object>>();
        setProperties(propertyList);
        String column = properties.replaceAll(";", ",");
        // 属性列
        column = column.substring(0, column.length() - 1);
        List<Map<String, Object>> list = stuDao.getStudentList(role, column, page, pageSize);
        return list;
    }

    /**
     * 根据角色获得属性list
     * 
     * @param role
     * @return
     */
    public List<Map<String, Object>> getProperty(String role) {
        StudentDao stuDao = new StudentDao();
        String properties = stuDao.getPropertiesByRole(role);
        if (!properties.contains("name;")) {
            properties = "name;" + properties;
        }
        if (!properties.contains("sex;")) {
            properties = properties.replace("name;", "name;sex;");
        }
        String propertyIn = "";
        String[] property = properties.split(";");
        for (int i = 0; i < property.length; i++) {
            if (i == property.length - 1) {
                propertyIn += "'" + property[i] + "'";
            } else {
                propertyIn += "'" + property[i] + "',";
            }
        }
        List<Map<String, Object>> propertyList = stuDao.getProperties(propertyIn);
        return propertyList;
    }
}
